package com.stylefeng.guns.modular.system.task;

import java.net.URL;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.http.client.methods.HttpGet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.core.common.constant.state.HsgtType;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.HttpClientUtil;
import com.stylefeng.guns.modular.system.constants.ImportContants;
import com.stylefeng.guns.modular.system.dao.HsgtMapper;
import com.stylefeng.guns.modular.system.model.Hsgt;
import com.stylefeng.guns.modular.system.model.HsgtData;

@Service
public class HsgtImportTask {

	private Logger log = LoggerFactory.getLogger(HsgtImportTask.class);
	
	@Autowired
	private HsgtMapper hsgtMapper;
	
	private static final HttpClientUtil clientUtil = new HttpClientUtil();
	
	public void execute(boolean isHoliday,HsgtType type) {
		if(isHoliday){
			if(HsgtType.A == type || HsgtType.B == type){
				String url = ImportContants.getHsgtDataPathStr(1, type.getCode());
				HttpGet httpGet = HttpClientUtil.getGetRequest();
				try {
					httpGet.setURI(new URL(url).toURI());
					String json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
					JSONObject jsonObj = JSON.parseObject(json);
					if (jsonObj.getBooleanValue("success")) {
						int pages = jsonObj.getIntValue("pages");
						parseHsgtData(jsonObj.getString("data"),type.getCode());
						for(int i= 2; i <= pages ; i++){
							url = ImportContants.getHsgtDataPathStr(i, type.getCode());
							httpGet.setURI(new URL(url).toURI());
							json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
							jsonObj = JSON.parseObject(json);
							if (jsonObj.getBooleanValue("success")) {
								parseHsgtData(jsonObj.getString("data"),type.getCode());
							}
						}
					}
				} catch (Exception e) {
					log.error("get hsgt data is error!" + e);
				}finally{
					httpGet.abort();
				}
			}else if(HsgtType.C == type){
				String startDate = DateUtil.format(new Date(), "yyyy-MM-dd");
				String endDate = DateUtil.format(new Date(), "yyyy-MM-dd");
				String url = ImportContants.getJgzyDataPathStr(1, startDate,endDate);
				HttpGet httpGet = HttpClientUtil.getGetRequest();
				try {
					httpGet.setURI(new URL(url).toURI());
					String json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
					JSONObject jsonObj = JSON.parseObject(StringUtils.substringAfter(json, "="));
					if (jsonObj.getBooleanValue("success")) {
						int pages = jsonObj.getIntValue("pages");
						parseJgzyData(jsonObj.getString("data"),type.getCode());
						for(int i= 2; i <= pages ; i++){
							url = ImportContants.getJgzyDataPathStr(i, startDate,endDate);
							httpGet.setURI(new URL(url).toURI());
							json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
							jsonObj = JSON.parseObject(StringUtils.substringAfter(json, "="));
							if (jsonObj.getBooleanValue("success")) {
								parseJgzyData(jsonObj.getString("data"),type.getCode());
							}
						}
					}
				} catch (Exception e) {
					log.error("get jgzy data is error!" + e);
				}finally{
					httpGet.abort();
				}
			}
		}
	}
	
	private void parseJgzyData(String json,int salesCode){
		if(StringUtils.isNotEmpty(json)){
			List<HsgtData> channelItemList = JSON.parseArray(json,HsgtData.class);
			for(HsgtData hsgtData : channelItemList){
				Hsgt hsgt = new Hsgt();
				hsgt.setgScode(hsgtData.getSCode());
				hsgt.setgTdate(hsgtData.getTDate());
				hsgt.setgSalesCode(salesCode);
				hsgt = hsgtMapper.selectByDate(hsgt);
				if(hsgt == null){
					hsgt = new Hsgt();
					hsgt.setgScode(hsgtData.getSCode());
					hsgt.setgTdate(hsgtData.getTDate());
					hsgt.setgBmoney(hsgtData.getBMoney());
					hsgt.setgPbuy(hsgtData.getPBuy());
					hsgt.setgSalesCode(salesCode);
					hsgt.setgSalesName("机构专用");
					hsgt.setgSmoney(hsgtData.getSMoney());
					hsgtMapper.insert(hsgt);
				}
			}
		}
	}
	
	private void parseHsgtData(String json,int salesCode){
		if(StringUtils.isNotEmpty(json)){
			List<HsgtData> channelItemList = JSON.parseArray(json,HsgtData.class);
			for(HsgtData hsgtData : channelItemList){
				Hsgt hsgt = new Hsgt();
				hsgt.setgScode(hsgtData.getSCode());
				hsgt.setgTdate(hsgtData.getTDate());
				hsgt.setgSalesCode(salesCode);
				hsgt = hsgtMapper.selectByDate(hsgt);
				if(hsgt == null){
					hsgt = new Hsgt();
					hsgt.setgScode(hsgtData.getSCode());
					hsgt.setgTdate(hsgtData.getTDate());
					hsgt.setgBmoney(hsgtData.getBMoney());
					hsgt.setgPbuy(hsgtData.getPBuy());
					hsgt.setgSalesCode(salesCode);
					hsgt.setgSalesName(hsgtData.getSalesName());
					hsgt.setgSmoney(hsgtData.getSMoney());
					hsgtMapper.insert(hsgt);
				}
			}
		}
	}
}
